<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
// 控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(false)
// 定义数据模型
const registerData = ref({
    username: '',
    password: '',
    /* rePassword: '' */
})

/* // 校验密码的函数
const checkRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== registerData.value.password) {
        callback(new Error('请确保两次输入的密码一样'))
    } else {
        callback()
    }
} */

// 定义表单校验规则
const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
   /*  rePassword: [
        { validator: checkRePassword, trigger: 'blur' }
    ] */
}

// 调用后台接口,完成注册
import { userRegisterService, userLoginService } from '../api/user.js'
const register = async () => {
    let result = await userRegisterService(registerData.value);
    ElMessage.success(result.msg ? result.msg : '注册成功')
}
import {adminLoginService} from '../api/admin.js'
/** 
 * 管理员登录
*/
const adminLogin = async () => {
    // 调用接口
    let result = await adminLoginService(registerData.value);
    ElMessage.success(result.msg ? result.msg : '登录成功')
    tokenStore.setToken(result.data)
    // 跳转到首页
    router.push('/admin')
}
import { getNodeInfoService } from '@/api/nodeInfo.js'
import useUserInfoStore from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();


// 登录函数
import { useTokenStore } from '../stores/token.js'
import { useRouter } from 'vue-router'
const router = useRouter()
const tokenStore = useTokenStore();
const login = async () => {
    try {
        // 用户登录
        let result = await userLoginService(registerData.value);
        ElMessage.success(result.msg ? result.msg : '登录成功')
        tokenStore.setToken(result.data)
        
        // 获取节点信息
        const nodeInfo = await getNodeInfoService()
        userInfoStore.setInfo(nodeInfo.data)
        
        // 根据节点类型跳转到不同页面
        switch(nodeInfo.data.type) {
            case 1:
                router.push('/ent/breeding')
                break
            case 2:
                router.push('/ent/slaghter')
                break
            case 3:
                router.push('/ent/farm')
                break
            case 4:
                router.push('/ent/market')
                break
        }
    } catch (error) {
        ElMessage.error(error.response?.data?.msg || '登录失败')
    }
}

// 定义函数,清空数据模型的数据
const clearRegisterData = () => {
    registerData.value = {
        username: '',
        password: '',
        rePassword: ''
    }
}
</script>

<template>
    <el-row class="login-page">
        <!-- 表单容器 -->
        <el-col :span="6" class="form-container">
            <el-card class="box-card">
                <!-- 注册表单 -->
                <el-form v-if="isRegister" ref="form" size="large" autocomplete="off" :model="registerData" :rules="rules">
                    <el-form-item>
                        <h1>管理员登录</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="button" type="primary" auto-insert-space @click="adminLogin">登录</el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
                            ← 返回
                        </el-link>
                    </el-form-item>
                </el-form>
                <!-- 登录表单 -->
                <el-form v-else ref="form" size="large" autocomplete="off" :model="registerData" :rules="rules">
                    <el-form-item>
                        <h1>登录</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
                    </el-form-item>
                    <el-form-item class="flex">
                        <div class="flex">
                            <el-checkbox>记住我</el-checkbox>
                            <el-link type="primary" :underline="false">忘记密码？</el-link>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
                            管理员登陆
                        </el-link>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-col>
    </el-row>
</template>

<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
    position: relative; /* 确保背景元素的定位正确 */
    background-image: url('@/assets/login_bg.png'); /* 预留背景图片 URL 位置 */
    background-size: cover; /* 使背景图片覆盖整个页面 */
    background-position: center; /* 居中显示背景图片 */
}

.form-container {
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1;
}

.box-card {
    width: 100%;
    
    max-width: 400px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background: #fff;
}

.button {
    width: 100%;
}

.flex {
    width: 100%;
    display: flex;
    justify-content: space-between;
}
</style>